package com.itools.core.context;

import com.itools.core.em.StrategyType;
import com.itools.core.service.FileManagerService;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @project: itools-backend
 * @description:
 * @author: XUCHANG
 * @create: 2021-04-01 16:02
 */
@Component
@Log4j2
public class FileStrategyServiceContext {
    @Autowired
    private Environment environment;
    @Getter
    private final static Map<String, FileManagerService> fileServiceMap = new HashMap<>();

    public FileManagerService get(String type) {
        return fileServiceMap.get(type);
    }

    /**
     * 获取默认或者配置的文件服务策略
     * @return
     */
    public FileManagerService get(){
        boolean containsProperty = environment.containsProperty("fms.strategy");
        if (!containsProperty){
            log.info("默认使用NIO策略");
            return fileServiceMap.get(StrategyType.NIO.getType());
        }
        String strategy = environment.getProperty("fms.strategy");
        if (!fileServiceMap.containsKey(strategy)){
            log.info("输入的文件系统策略不存在，请输入合法文件策略，默认使用NIO策略");
            return fileServiceMap.get(StrategyType.NIO.getType());
        }
        log.info("文件系统策略使用{}策略",strategy);
        return fileServiceMap.get(strategy);
    }

    public void put(String type, FileManagerService calcService) {
        fileServiceMap.put(type, calcService);
    }
    public Map<String, FileManagerService> getMap(){
        return fileServiceMap;
    }
}
